# !/usr/bin/env python
# !-*-coding:utf-8 -*-
# !@TIME: 
# !@Author : Destroy丶寒露
# !@File :.py

import time
from selenium.webdriver.common.keys import Keys

class Lagou:

    def __init__(self, driver):
        self.driver = driver

    def start(self, visitweb):
        dinwei_btn = self.driver.find_element_by_class_name('tab.focus')
        dinwei_btn.click()
        time.sleep(3)
        input_tag = self.driver.find_element_by_id('search_input')
        key = 'java'
        input_tag.send_keys(key)
        input_tag.send_keys(Keys.ENTER)
        time.sleep(6)
        self.get_item(visitweb)

    def get_item(self, visitweb):
        item_list = self.driver.find_elements_by_class_name('con_list_item.default_list')
        for item in item_list:
            item_url = item.find_element_by_tag_name('a').get_attribute('href')
            item_position = item.find_element_by_css_selector('.position_link h3').text
            item_money = item.find_element_by_css_selector('.money').text
            item_company = item.find_element_by_css_selector('.company_name a').text
            item_info = '''
                职位：%s
                链接：%s       
                薪资：%s
                公司：%s
            ''' % (item_position, item_url, item_money, item_company)
            print(item_position)
            item_info_list = {'1_No': '', '2_position': '', '3_link': '', '4_money': '', '5_company': ''}
            info = [visitweb.data_num, item_position, item_url, item_money, item_company]
            visitweb.data_num += 1
            item_info_list = dict(zip(item_info_list.keys(), info))
            visitweb.info_list.append(item_info_list)
        try:
            self.next_page(visitweb)  # 翻页
        except Exception as e:
            print('已到末页！')

    def next_page(self, visitweb):
        next_page = self.driver.find_element_by_class_name('pager_next')
        next_page.click()
        time.sleep(3)
        self.get_item(visitweb)
